# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class T1Pipeline:

    def close_spider(self,spider):
        self.file.close()

    def process_item(self, item, spider):
        book = None
        res = dict(item)
        if res['name'] != book:
            if book is not None:
                self.file.close()
            book = res['name']
            self.file = open(book+'.txt', 'a', encoding='utf8')
        directory = res['directory']
        content = res['content']
        # print(directory)
        line = directory + '\n'+content+'\n'
        self.file.write(line)
